package com.mairuide.bill.titlepool;

import java.util.*;

import javax.annotation.Resource;

import org.apache.commons.collections4.IterableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.base.Constant;
import com.mairuide._frame.base.ModuleCode;
import com.mairuide._frame.base.PageSupport;
import com.mairuide._frame.utils.SecurityUtil;
import com.mairuide._frame.utils.Tools;
import com.mairuide.datalog.ActionType;
import com.mairuide.datalog.DataOperationLogService;
import com.mairuide.datalog.ModuleFlag;
import com.mairuide.datalog.SystemFlag;
import com.mairuide.platmanager.PlatManager;

@Controller
@RequestMapping("/bill/titlepool")
public class DealFlowTitlePoolController extends BaseController {

    @Autowired
    private DealFlowTitlePoolService dealFlowTitlePoolService;
    
    @Resource
    private DataOperationLogService dataOperationLogService;
    
	@RequestMapping(value = "/showTitlePool", method = RequestMethod.GET)
	public Object importFile(ModelMap model) {
		DealFlowTitleHead [] dealFlowTitleHeads = DealFlowTitleHead.values();
        List<DealFlowTitle> titleList = new ArrayList<>();
        for (DealFlowTitleHead pgh:dealFlowTitleHeads){
            Integer id = pgh.getOldHead();
            String headTitle = pgh.getNewHead();
            DealFlowTitle dealFlowTitle = new DealFlowTitle();
            dealFlowTitle.setId(id);
            dealFlowTitle.setMappingField(headTitle.split(",")[1]);
            titleList.add(dealFlowTitle);
        }
        model.addAttribute("titleList",titleList);
		return "/casemanage/showTitlePool";
	}
	
    /**
     * 获取标题列表
     * @param model
     * @return
     */
    @ModuleCode(pValue = "billanalysis", value = "billTitle")
    @RequestMapping(value = "/title",method = RequestMethod.GET)
    public Object getTitleList(ModelMap model){
        DealFlowTitleHead [] dealFlowTitleHeads = DealFlowTitleHead.values();
        List<DealFlowTitle> titleList = new ArrayList<>();
        for (DealFlowTitleHead pgh:dealFlowTitleHeads){
            Integer id = pgh.getOldHead();
            String headTitle = pgh.getNewHead();
            DealFlowTitle dealFlowTitle = new DealFlowTitle();
            dealFlowTitle.setId(id);
            dealFlowTitle.setMappingField(headTitle.split(",")[1]);
            titleList.add(dealFlowTitle);
        }
        model.addAttribute("titleList",titleList);
        return "/bill/titlepool";
    }

    /**
     * 获取标题列表集合
     * @param
     * @return
     */
    @RequestMapping(value = "/titleList",method = RequestMethod.GET)
    @ResponseBody
    public List<DealFlowTitle> getAjaxTitleList(){
        DealFlowTitleHead [] dealFlowTitleHeads = DealFlowTitleHead.values();
        List<DealFlowTitle> titleList = new ArrayList<>();
        for (DealFlowTitleHead pgh:dealFlowTitleHeads){
            Integer id = pgh.getOldHead();
            String headTitle = pgh.getNewHead();
            DealFlowTitle dealFlowTitle = new DealFlowTitle();
            dealFlowTitle.setId(id);
            dealFlowTitle.setMappingField(headTitle.split(",")[1]);
            titleList.add(dealFlowTitle);
        }
        return titleList;
    }

    /**
     * 模糊匹配查询标题
     * @param key
     * @return
     */
    @RequestMapping(value = "/querytitle",method = RequestMethod.GET)
    @ResponseBody
    public List<DealFlowTitlePool> queryTitle(String key){
        key = key.trim();
        List<DealFlowTitlePool> byBankTitleNameLike = dealFlowTitlePoolService.findByBankTitleNameLike("%"+key+"%");
        return byBankTitleNameLike;
    }

    /**
     * 获得标题对应的标题池列表
     * @param
     * @return
     */
    @RequestMapping(value = "/titlepool",method = RequestMethod.GET)
    @ResponseBody
    public List<DealFlowTitlePool> getTitlePoolList(Integer id,String order){
        List<DealFlowTitlePool> titlePoolList=null;
        if (id==null||id==0){
        }else {
            titlePoolList = dealFlowTitlePoolService.getTitlePoolList(id);
        }
        return titlePoolList;
    }

    /**
     * 添加标题
     * @param dealFlowTitlePool
     * @param
     * @throws Exception 
     */
    @RequestMapping(value = "/saveAdd",method = RequestMethod.POST)
    @ResponseBody
    public Object saveAddTitle(DealFlowTitlePool dealFlowTitlePool) throws Exception{
        Integer managerId = super.getLoginUser().getId();
//        String banktitle = SecurityUtil.encrypt(dealFlowTitlePool.getBankTitleName());
//        dealFlowTitlePool.setBankTitleName(banktitle);
        Map<String ,Object> model = new HashMap<>();
        boolean query = dealFlowTitlePoolService.queryBankTitleName(dealFlowTitlePool.getBankTitleName());
        if (query){
            model.put("msg","标题已经存在");
        }else {
            dealFlowTitlePool.setCreateId(managerId);
            dealFlowTitlePool.setCreateTime(new Date());
            dealFlowTitlePool.setIsDelete(0);
            dealFlowTitlePool.setClassName("com.mairuide.bill.DealFlow");
            String types = DealFlowTitleHead.getNewHeadByOldHead(dealFlowTitlePool.getTitleId());
            dealFlowTitlePool.setClassField(types.split(",")[0]);
            dealFlowTitlePool.setFieldType(Integer.parseInt(types.split(",")[2]));
            dealFlowTitlePoolService.save(dealFlowTitlePool);
            model.put("msg","添加成功");
            dataOperationLogService.save(dealFlowTitlePool.getId().toString(), super.getLoginUser(), dealFlowTitlePool, new DealFlowTitlePool(), SystemFlag.ZD, ModuleFlag.DEALFLOWTITLEPOOL, ActionType.ADD);
        }
        //插入添加日志
        return SUCCESS(model);
    }

    /**
     * 修改标题
     * @param dealFlowTitlePool
     * @param
     * @throws Exception 
     */
    @RequestMapping(value = "/saveUpdate",method = RequestMethod.POST)
    @ResponseBody
    public Object saveTitle(DealFlowTitlePool dealFlowTitlePool) throws Exception{
    	PlatManager pm = super.getLoginUser();
    	Integer managerId = pm.getId();
//        String banktitle = SecurityUtil.encrypt(dealFlowTitlePool.getBankTitleName());
//        dealFlowTitlePool.setBankTitleName(banktitle);
        DealFlowTitlePool cpdftp = Tools.beanCopyForLodBean(dealFlowTitlePool);
        Map<String ,Object> model = new HashMap<>();
        DealFlowTitlePool one = dealFlowTitlePoolService.findOne(dealFlowTitlePool.getId());
        if (managerId == 1 || one.getCreateId() == managerId){//管理员
            one.setBankTitleName(dealFlowTitlePool.getBankTitleName());
            dealFlowTitlePoolService.save(one);
            model.put("msg","修改成功");
        }else {
        	model.put("msg","没有修改此条记录权限");
        }
        //添加修改日志
        dataOperationLogService.save(dealFlowTitlePool.getId().toString(), pm, one, cpdftp, SystemFlag.ZD, ModuleFlag.DEALFLOWTITLEPOOL, ActionType.MODIFY);
        return SUCCESS(model);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteTitle(Integer id,ModelMap model) throws Exception{
    	DealFlowTitlePool dftp = dealFlowTitlePoolService.findOne(id);
    	DealFlowTitlePool cpdftp = Tools.beanCopyForLodBean(dftp);
    	dftp.setIsDelete(Constant.DELETED);
    	dftp = dealFlowTitlePoolService.save(dftp);
        model.addAttribute("msg","删除成功");
        //添加删除日志
        dataOperationLogService.save(dftp.getId().toString(), super.getLoginUser(), dftp, cpdftp, SystemFlag.ZD, ModuleFlag.DEALFLOWTITLEPOOL, ActionType.DELETE);
        return SUCCESS(model);
    }

}
